local skel = fk.CreateSkill {
  name = "lb_zero__guanshou",
}

Fk:loadTranslationTable {
  ["lb_zero__guanshou"] = "观首",
  [":lb_zero__guanshou"] = "你每回合首次使用牌时，你可令一名目标或当前回合角色选择一项：1.将所有手牌移出游戏，直至本回合结束；2.此后其回合开始时，你可以使用一张牌；3.移除前一项对其的效果，令你摸两张牌。",

  ["#lb_zero__guanshou"] = "观首：你可以令 %src 进行〖观首〗选择",
  ["#lb_zero__guanshou-choose"] = "观首：你可以令一名角色进行〖观首〗选择",
  ["#lb_zero__guanshou-use"] = "观首：你可以使用一张牌",

  ["@[player_chara]lb_zero__guanshou"] = "观首",
  ["$lb_zero__guanshou"] = "观首",

  ["#lb_zero__guanshou_1"] = "将所有手牌移出游戏，直至本回合结束",
  ["#lb_zero__guanshou_2"] = "此后你回合开始时，%src可以使用一张牌",
  ["#lb_zero__guanshou_3"] = "移除前一项对你的效果，令%src摸两张牌",

  ["$lb_zero__guanshou1"] = "福福生威。",
  ["$lb_zero__guanshou2"] = "可怕，又威风！",
  ["$lb_zero__guanshou3"] = "全都打飞！",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local room = player.room
      local use_events = room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        return e.data.from == player
      end, Player.HistoryTurn)
      return #use_events == 1 and use_events[1].data == data
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = table.simpleClone(data.tos)
    if room:getCurrent() then
      table.insertIfNeed(tos, room:getCurrent())
    end
    if #tos == 0 then return end
    if #tos > 1 then
      tos = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        cancelable = true,
        prompt = "#lb_zero__guanshou-choose",
      })
      if #tos == 0 then return end
    else
      if not room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__guanshou:" .. tos[1].id }) then
        return
      end
    end
    event:setCostData(self, { tos = tos })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local all_choices = { "#lb_zero__guanshou_1", "#lb_zero__guanshou_2:" .. player.id, "#lb_zero__guanshou_3:" .. player.id }
    local choices = table.simpleClone(all_choices)
    if table.contains(player:getTableMark("@[player_chara]lb_zero__guanshou"), to.id) then
      table.remove(choices, 2)
    else
      table.remove(choices, 3)
    end
    if to:isKongcheng() then
      table.remove(choices, 1)
    end
    local choice = room:askToChoice(to, {
      choices = choices,
      all_choices = all_choices,
      skill_name = skel.name,
    })
    local index = table.indexOf(all_choices, choice)
    if index == 1 then
      to:addToPile("$lb_zero__guanshou", to:getCardIds("h"), false, skel.name, to)
    elseif index == 2 then
      room:addTableMark(player, "@[player_chara]lb_zero__guanshou", to.id)
    elseif index == 3 then
      room:removeTableMark(player, "@[player_chara]lb_zero__guanshou", to.id)
      player:drawCards(2, skel.name)
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and table.contains(player:getTableMark("@[player_chara]lb_zero__guanshou"), target.id)
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToPlayCard(player, {
      pattern = ".",
      skill_name = skel.name,
      prompt = "#lb_zero__guanshou-use",
      skip = true,
    })
    if use then
      event:setCostData(self, { use = use })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).use)
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and #player:getPile("$lb_zero__guanshou") > 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(player:getPile("$lb_zero__guanshou"), Player.Hand, player, fk.ReasonPrey, skel.name)
  end,
})

return skel
